package employeeOperations;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import profileEditor.ProfileEditor;

import data.EmployeeInfo;

import storage.Employee;

import GUIMain.GUIFocusManager;

public class SelectEmployeeDialog {
	private static JDialog dialog;
	private static JTable rightTable;
	private static JList leftList;
	private static DefaultListModel leftModel;
	private static JScrollPane leftScrollPane;
	private static JScrollPane rightScrollPane;
	private static GridBagConstraints gbc;
	private static HashMap<String,ArrayList<Employee>> employeeMap;
	private static HashMap<String,ArrayList<String>> activatedEmployeeMap;
	private static String currentActivatedLocation;
	private static JButton cancel;
	private static JButton submit;
	private static EmployeeTableModel etm;
	private static boolean origin;
	
	public static void open(boolean origin1){
		origin = origin1;
		employeeMap.clear();
		activatedEmployeeMap.clear();
		etm.removeAllRows();
		currentActivatedLocation = null;
		if(origin){
			ArrayList<String> originLocationList = EmployeeInfo.getOriginLocations();
			for(int i=0;i<originLocationList.size();i++){
				employeeMap.put(originLocationList.get(i), EmployeeInfo.getEmployees(originLocationList.get(i)));
				activatedEmployeeMap = ProfileEditor.getOriginEmployees();
			}
		}else{
			ArrayList<String> destLocationList = EmployeeInfo.getDestLocations();
			for(int i=0;i<destLocationList.size();i++){
				employeeMap.put(destLocationList.get(i), EmployeeInfo.getEmployees(destLocationList.get(i)));
				activatedEmployeeMap = ProfileEditor.getDestEmployees();
			}
		}
		if(activatedEmployeeMap==null){
			activatedEmployeeMap = new HashMap<String,ArrayList<String>>();
		}
		updateLeftList();
		GUIFocusManager.add(dialog);
	}
	private static void updateLeftList() {
		leftModel.clear();
		Object[] keyList = employeeMap.keySet().toArray();
		for(int i=0;i<keyList.length;i++){
			leftModel.addElement(keyList[i]);
		}
		leftList.setSelectedIndex(-1);
		leftScrollPane.repaint();
	}
	public static void initialize(){
		dialog = new JDialog();
		dialog.setLayout(new GridBagLayout());
		dialog.setTitle("Select Employees");
		currentActivatedLocation = null;
		employeeMap = new HashMap<String,ArrayList<Employee>>();
		activatedEmployeeMap = new HashMap<String,ArrayList<String>>();
		
		leftModel = new DefaultListModel();
		leftList = new JList(leftModel);
		leftScrollPane = new JScrollPane(leftList);
		leftScrollPane.setPreferredSize(new Dimension(250,400));
		
		etm = new EmployeeTableModel(false);
		rightTable = new JTable(etm);
		rightTable.getColumnModel().getColumn(0).setHeaderValue("Name");
		rightTable.getColumnModel().getColumn(1).setHeaderValue("Email");
		rightTable.getColumnModel().getColumn(2).setHeaderValue("Select");
		rightScrollPane = new JScrollPane(rightTable);
		rightScrollPane.setPreferredSize(new Dimension(450,400));
		
		gbc = new GridBagConstraints();
		cancel = new JButton("Cancel");
		submit = new JButton("Submit");
		
		gbc.insets.top =3; 
		gbc.insets.bottom = 3;
		gbc.insets.right = 3;
		gbc.insets.left = 3;
		
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.gridx = 0;
		gbc.gridy = 0;
		dialog.add(leftScrollPane,gbc);
		
		gbc.gridx =1;
		gbc.gridy = 0;
		dialog.add(rightScrollPane,gbc);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridBagLayout());
		
		gbc.gridx = 0;
		gbc.gridy = 0;
		buttonPanel.add(cancel,gbc);
		gbc.gridx = 1;
		buttonPanel.add(submit,gbc);
		
		gbc.gridwidth =2;
		gbc.gridy = 1;
		gbc.gridx = 0;
		dialog.add(buttonPanel,gbc);
		
		attachListeners();
		
		GUIFocusManager.setDialogProperties(dialog);
	}
	public static void attachListeners(){
		leftList.addListSelectionListener(new ListSelectionListener(){
			@Override
			public void valueChanged(ListSelectionEvent e) {
				//Upon detecting a list selection, update right list to display
				//employees for the new selection
				if(leftList.getSelectedIndex()>-1){
					//Save current list
					if(currentActivatedLocation!=null){
						//Store for output
						activatedEmployeeMap.put(currentActivatedLocation, etm.getSelectedValues());
					}
					etm.removeAllRows();
					currentActivatedLocation = (String) leftList.getSelectedValue();
					
					ArrayList<Employee> empList = employeeMap.get(currentActivatedLocation);
					
					for(int i=0;i<empList.size();i++){
						Vector<Object> v = new Vector<Object>();
						v.add(empList.get(i).name);
						v.add(empList.get(i).email);
						if(activatedEmployeeMap.get(currentActivatedLocation)!=null&&
								activatedEmployeeMap.get(currentActivatedLocation).contains(empList.get(i).name))
							v.add(new Boolean(true));
						else
							v.add(new Boolean(false));
						etm.addRow(v);
					}
				}
			}
		});
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUIFocusManager.remove();
			}
		});
		submit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				activatedEmployeeMap.put(currentActivatedLocation, etm.getSelectedValues());
				for(int i=activatedEmployeeMap.size()-1;i>=0;i--){
					if(activatedEmployeeMap.get(i)==null||activatedEmployeeMap.get(i).size()<1){
						activatedEmployeeMap.remove(i);
					}
				}
				if(origin){	
					ProfileEditor.updateOriginEmployees(activatedEmployeeMap);
				}else{
					ProfileEditor.updateDestEmployees(activatedEmployeeMap);
				}
				GUIFocusManager.remove();
			}
		});
	}
}
